package com.hj.system.system.controller;

import com.hj.system.base.annotation.SystemLog;
import com.hj.system.base.utils.PageUtils;
import com.hj.system.common.controller.BaseController;
import com.hj.system.base.utils.ResponseUtil;
import com.hj.system.system.domain.RoleDO;
import com.hj.system.system.service.RoleService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

@RequestMapping("/sys/role")
@Controller
public class RoleController extends BaseController {
    String prefix = "system/role";
    @Autowired
    RoleService roleService;

    @RequiresPermissions("sys:role:role")
    @GetMapping()
    public String role() {
        return prefix + "/role";
    }

    @RequiresPermissions("sys:role:role")
    @GetMapping("/list")
    @ResponseBody()
    public Map<String, Object> list(@RequestParam Map<String, Object> params) {
        PageUtils pageUtils = roleService.list(params);
        return ResponseUtil.okContent(pageUtils);
    }

    @RequiresPermissions(value = {"sys:role:add", "sys:role:edit"}, logical = Logical.OR)
    @GetMapping("/modify")
    public String modify(String roleId, Model model) {
        RoleDO roleDO;
        if (null != roleId && !"".equals(roleId)) {
            roleDO = roleService.get(roleId);
        } else {
            roleDO = new RoleDO();
        }
        model.addAttribute("role", roleDO);
        return prefix + "/modify";
    }

    @SystemLog("保存角色")
    @RequiresPermissions("sys:role:add")
    @PostMapping("/save")
    @ResponseBody
    public ResponseUtil save(RoleDO role) {
        role.setUserIdCreate(getUserId());
        role.setGmtCreate(new Date());
        if (roleService.save(role) > 0) {
            return ResponseUtil.ok();
        } else {
            return ResponseUtil.error(1, "保存失败");
        }
    }

    @SystemLog("更新角色")
    @RequiresPermissions("sys:role:edit")
    @PostMapping("/update")
    @ResponseBody
    public ResponseUtil update(RoleDO role) {
        role.setGmtModified(new Date());
        if (roleService.update(role) > 0) {
            return ResponseUtil.ok();
        } else {
            return ResponseUtil.error(1, "保存失败");
        }
    }

    @SystemLog("删除角色")
    @RequiresPermissions("sys:role:remove")
    @PostMapping("/remove")
    @ResponseBody
    public ResponseUtil save(String roleId) {
        if (roleService.remove(roleId) > 0) {
            return ResponseUtil.ok();
        } else {
            return ResponseUtil.error(1, "删除失败");
        }
    }

    @RequiresPermissions("sys:role:batchRemove")
    @SystemLog("批量删除角色")
    @PostMapping("/batchRemove")
    @ResponseBody
    public ResponseUtil batchRemove(@RequestParam("ids[]") String[] ids) {
        int r = roleService.batchRemove(ids);
        if (r > 0) {
            return ResponseUtil.ok();
        }
        return ResponseUtil.error();
    }

    @RequiresPermissions("sys:role:grantPermissions")
    @SystemLog("给角色分配菜单")
    @PostMapping(value = "/grantPermissions")
    @ResponseBody
    public ResponseUtil grantPermissions(@RequestParam("menuIds[]") String[] menuIds, String roleId) {
        int r = roleService.grantPermissions(menuIds, roleId);
        if (r > 0) {
            return ResponseUtil.ok();
        }
        return ResponseUtil.error();
    }
}
